//
//  SeekController.m
//  HandheldHospital
//
//  Created by 张鹏的电脑 on 16/2/29.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "SeekController.h"
#import "HomeCell.h"
#import "DataManager.h"
#import "SeekModel.h"
#import <UIImageView+WebCache.h>
#import "CellRCConversationViewController.h"

#define kShared [DataManager defaultManager]

@interface SeekController ()<UISearchBarDelegate>

@property(nonatomic,strong) NSMutableArray *searchArray;

@property (nonatomic,strong) UISearchBar *searchBar;

@end

@implementation SeekController


-(NSMutableArray *)searchArray{
    if(_searchArray == nil)
    {
        _searchArray = [NSMutableArray array];
    }
    return _searchArray;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    //隐藏tabBar
    self.tabBarController.tabBar.hidden = YES;
    //设置tableView大小
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20,[UIScreen  mainScreen].bounds.size.width ,[UIScreen  mainScreen].bounds.size.height)];
    self.searchBar  = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 20, self.tableView.frame.size.width, 44)];
    [self.tableView addSubview:self.searchBar];
    self.searchBar.delegate = self;
   
    //注册 cell
    [self.tableView registerNib:[UINib nibWithNibName:@"HomeCell" bundle:nil]   forCellReuseIdentifier:@"cell"];
    //设置代理
     self.tableView.tableHeaderView = self.searchBar;
}




#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.searchArray.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"搜索结果";
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 150;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    SeekModel *seek = self.searchArray[indexPath.row];
    cell.nameLable.text = seek.name;
    cell.zhichenglabel.text = seek.zhicheng;
    cell.yiyuanLabel.text = seek.yiyuan;
    cell.keLeiLabel.text = seek.kelei;
    AVFile *file = seek.currentUser[@"Picture"];
    [cell.imaV sd_setImageWithURL:[NSURL URLWithString:file.url]];
    return cell;
    
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    if(self.searchArray.count > 0)
    {
        [self.searchArray removeAllObjects];
    }
    NSString *str = searchBar.text;
    for(SeekModel *seek in kShared.seekArray)
    {
        if([seek.name containsString:str])
        {
            [self.searchArray addObject:seek];
        }
    }
    //刷新表格
    [self.tableView reloadData];
    [searchBar resignFirstResponder];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if([kShared.userType isEqualToString:@"普通用户"]||[kShared.userType isEqualToString:@"医生"])
    {
        SeekModel *seek = self.searchArray[indexPath.row];
        CellRCConversationViewController *cellRCConversationVC = [[CellRCConversationViewController alloc] init];
        cellRCConversationVC.seek = seek;
        cellRCConversationVC.titleName = seek.name;
        [self.navigationController pushViewController:cellRCConversationVC animated:YES];
    }    
}





@end
